<?php

/**
 * concepto actions.
 *
 * @package    sf_sandbox
 * @subpackage concepto
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class conceptoActions extends sfActions
{
 public function executeShow(sfWebRequest $request)
  {
    $this->concepto = Doctrine_Core::getTable('Concepto')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->concepto);
  }

  public function executeIndex(sfWebRequest $request)
  {
       $this->conceptos = $this->getPager(ConceptoTable::getBuscarConceptos(""), $request->getGetParameter('page'), "Concepto");
//    $this->conceptos = Doctrine_Core::getTable('Concepto')
//      ->createQuery('a')
//      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ConceptoForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ConceptoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($concepto = Doctrine_Core::getTable('Concepto')->find(array($request->getParameter('id'))), sprintf('Object concepto does not exist (%s).', $request->getParameter('id')));
    $this->form = new ConceptoForm($concepto);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($concepto = Doctrine_Core::getTable('Concepto')->find(array($request->getParameter('id'))), sprintf('Object concepto does not exist (%s).', $request->getParameter('id')));
    $this->form = new ConceptoForm($concepto);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($concepto = Doctrine_Core::getTable('Concepto')->find(array($request->getParameter('id'))), sprintf('Object concepto does not exist (%s).', $request->getParameter('id')));
    $concepto->delete();

    $this->redirect('concepto/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $concepto = $form->save();

      $this->redirect('concepto/index');
    }
  }
  
  public function executeEliminar(sfWebRequest $request) {
        $this->forward404Unless($this->concepto  = Doctrine_Core::getTable('Concepto')->find(array($request->getParameter('id'))), sprintf('Object concepto does not exist (%s).', $request->getParameter('id')));            
       
    }
 
      protected function getPager($q, $page, $model) {
        $pager = new sfDoctrinePager($model, sfConfig::get('app_nro_x_pagina_contribuyente'));
        $pager->setQuery($q);
        $pager->setPage($page);
        $pager->init();

        return $pager;
    }
    public function executeBuscarConceptos(sfWebRequest $request) {
        
        if ($request->isMethod('post')) {
            $nombre = $request->getPostParameter("nombre");
           } else {
            $nombre = $request->getGetParameter("nombre");
          }
        $this->conceptos = $this->getPager(ConceptoTable::getBuscarConceptos($nombre), $request->getGetParameter('page'), "Concepto");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("concepto/listadoconceptos", array('conceptos' => $this->conceptos));
        }
    }
    
    
}
